exports.addBulkPurchaseActivity = function (req, res) {

    //获取restful参数
    var bodyParams = req.routeInfo.bodyParams;

    //最终结果
    var resultData = {};

    var new_oaid = 0;

    var completeCount = 0;

    var successCount = 0;

    var cash_coupon_id = 0;

    //任务流创建
    async.waterfall([
        //1.权限验证
        /*function (cb) {
            P.permissionValidation(req, res, [CORE_APIConfig.coreAPI.addOrganizationActivity], function () {
                cb()
            });
        },*/

            //添加活动基本信息
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url   : CORE_APIConfig.coreAPI.addOrganizationActivity,
                        method: 'POST',
                        data  : {
                            oa_title        : bodyParams.activity_name,
                            oa_type         : "0",
                            oa_oid          : bodyParams.oid,
                            oa_category_text:bodyParams.oa_category_text == undefined ? "" : bodyParams.oa_category_text,
                            oa_start_time   : bodyParams.activity_start_time,
                            oa_end_time     : bodyParams.activity_end_time,
                            oa_title_image  : bodyParams.activity_image,
                            oa_title_image_text: bodyParams.oa_title_image_text == undefined ? "" : bodyParams.oa_title_image_text,
                            oa_title_image_text_style: bodyParams.oa_title_image_text_style == undefined ? "" : bodyParams.oa_title_image_text_style,

                            oa_skin_id: bodyParams.oa_skin_id   == undefined ? "" : bodyParams.oa_skin_id,
                            oa_music_id: bodyParams.oa_music_id == undefined ? "" : bodyParams.oa_music_id,

                            oa_class_time: bodyParams.oa_class_time == undefined ? "" : bodyParams.oa_class_time,
                            oa_class_duration: bodyParams.oa_class_duration == undefined ? "" : bodyParams.oa_class_duration,
                            oa_class_apply: bodyParams.oa_class_apply == undefined ? "" : bodyParams.oa_class_apply,
                            oa_class_people: bodyParams.oa_class_people == undefined ? "" : bodyParams.oa_class_people,
                            oa_class_basics: bodyParams.oa_class_basics == undefined ? "" : bodyParams.oa_class_basics,
                            oa_class_phone: bodyParams.oa_class_phone == undefined ? "" : bodyParams.oa_class_phone,
                            oa_thumbnail: bodyParams.oa_thumbnail == undefined ? "" : bodyParams.oa_thumbnail,
                            oa_title_image_type: bodyParams.oa_title_image_type == undefined ? "" : bodyParams.oa_title_image_type,
                            min_count              : bodyParams.min_count,
                            open_price              : bodyParams.open_price,
                        }
                    },
                    function (err, data) {
                        console.log(111,data);
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "拼团活动新增失败，请重新尝试");
                        }
                        new_oaid = data;
                        cb();
                    }
                );

            },

            //添加活动扩展信息
            function (cb) {

                //console.log("+++++++++")
                //console.log(bodyParams.old_price)

                R.SEND_HTTP(
                    req,
                    {
                        url   : CORE_APIConfig.coreAPI.addBulkPurchaseActivityConfig,
                        method: 'POST',
                        data  : {
                            bpac_oaid                   : new_oaid,
                            bpac_old_price              : bodyParams.old_price,
                            bpac_open_price             : bodyParams.open_price,
                            bpac_type                   : bodyParams.bpac_type,
                            bpac_participation_price    : bodyParams.participation_price,
                            bpac_min_count              : bodyParams.min_count,
                            // bpac_appointment_info       : bodyParams.bpac_appointment_info,
                            // bpac_class_adjust_info      : bodyParams.bpac_class_adjust_info,
                            // bpac_refund_info            : bodyParams.refund_info,
                            // bpac_other_info             : bodyParams.other_info,
                            bpac_max_user_count         : bodyParams.bpac_max_user_count,
                            
                            bpac_builder_coupon_id      : bodyParams.builder_coupon_id=='undefined' || bodyParams.builder_coupon_id==undefined?'':bodyParams.builder_coupon_id,
                            bpac_builder_coupon_type    : bodyParams.builder_coupon_type=='undefined' || bodyParams.builder_coupon_type==undefined?'':bodyParams.builder_coupon_type,
                            bpac_builder_coupon_name    : bodyParams.builder_coupon_name=='undefined' || bodyParams.builder_coupon_name==undefined?'':bodyParams.builder_coupon_name,
                            bpac_participant_coupon_id  : bodyParams.participant_coupon_id=='undefined' || bodyParams.participant_coupon_id==undefined?'':bodyParams.participant_coupon_id,
                            bpac_participant_coupon_type: bodyParams.participant_coupon_type=='undefined' || bodyParams.participant_coupon_type==undefined?'':bodyParams.participant_coupon_type,
                            bpac_participant_coupon_name: bodyParams.participant_coupon_name=='undefined' || bodyParams.participant_coupon_name==undefined?'':bodyParams.participant_coupon_name
                            // bpac_prompt : bodyParams.bpac_prompt

                            // bc_course_image : bodyParams.bc_course_image,
                            // bc_course_title : bodyParams.bc_course_title,
                            // bc_course_price : bodyParams.bc_course_price,
                            // bc_course_count : bodyParams.bc_course_count,
                            // bc_course_desc  : bodyParams.bc_course_desc
                        }
                    },
                    function (err, data) {

                        console.log(222,data);
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, "拼团活动新增失败，请重新尝试");
                        }

                        cb();
                    }
                );

            },
            /************************************************************************/
            //添加开团优惠券的记录
            function (cb) {
                if(bodyParams.bpac_type == 0){
                if(bodyParams.builder_coupon_id != undefined && bodyParams.builder_coupon_id!=""&&bodyParams.builder_coupon_id!=0){

                var builder_coupon_data = bodyParams.builder_coupon_id.split('|');
                var builder_coupon_typedata = bodyParams.builder_coupon_type.split('|');

                //创建添加任务
                var tasks = [];

                //循环节点列表
                builder_coupon_data.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req,
                            {
                                url   : CORE_APIConfig.coreAPI.addActivityCoupon,
                                method: 'POST',
                                data: {
                                    ac_oid       : bodyParams.oid,                //机构id
                                    ac_oaid      : new_oaid,                      //活动id
                                    ac_type      : builder_coupon_typedata[i],        //活动id
                                    ac_fid       : builder_coupon_data[i],        //券id
                                    ac_oatype    :3                              //活动类型
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                //判断是否有错误信息
                                if (err) {
                                    //console.log("添加错误");
                                }


                                cb();
                            }
                        );

                    });

                });

                async.waterfall(tasks, cb);

                }else{
                    cb();
                }
                }else{
                    R.SEND_HTTP(
                        req,
                        {
                            url   : CORE_APIConfig.coreAPI.addActivityCoupon,
                            method: 'POST',
                            data: {
                                ac_oid       : bodyParams.oid,                //机构id
                                ac_oaid      : new_oaid,                      //活动id
                                ac_type      : bodyParams.builder_coupon_id,        //活动id
                                ac_fid       : bodyParams.builder_coupon_type,        //券id
                                ac_oatype    :3                              //活动类型
                            }
                        },
                        function (err, data) {

                            //console.log(data);

                            //判断是否有错误信息
                            if (err) {
                                //console.log("添加错误");
                            }


                            cb();
                        }
                    );
                }
            },
            //添加拼团优惠券的记录
            function (cb) {
                if(bodyParams.bpac_type == 0){
                if(bodyParams.participant_coupon_id != undefined && bodyParams.participant_coupon_id !="" && bodyParams.participant_coupon_id !=0){

                var participant_coupon_data = bodyParams.participant_coupon_id.split('|');
                var participant_coupon_typedata = bodyParams.participant_coupon_type.split('|');


                //创建添加任务
                var tasks = [];

                //循环节点列表
                    participant_coupon_data.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req,
                            {
                                url   : CORE_APIConfig.coreAPI.addActivityCoupon,
                                method: 'POST',
                                data: {
                                    ac_oid       : bodyParams.oid,                //机构id
                                    ac_oaid      : new_oaid,                      //活动id
                                    ac_type      : participant_coupon_typedata[i],        //活动id
                                    ac_fid       : participant_coupon_data[i],        //券id
                                    ac_oatype    :4                             //活动类型
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                //判断是否有错误信息
                                if (err) {
                                    console.log("添加错误");
                                }
                                cb();
                            }
                        );

                    });

                });

                async.waterfall(tasks, cb);

                }else{
                    cb();
                }
                }else{
                    R.SEND_HTTP(
                        req,
                        {
                            url   : CORE_APIConfig.coreAPI.addActivityCoupon,
                            method: 'POST',
                            data: {
                                ac_oid       : bodyParams.oid,                //机构id
                                ac_oaid      : new_oaid,                      //活动id
                                ac_type      : bodyParams.participant_coupon_id,        //活动id
                                ac_fid       : bodyParams.participant_coupon_type,        //券id
                                ac_oatype    :4                             //活动类型
                            }
                        },
                        function (err, data) {

                            //console.log(data);

                            //判断是否有错误信息
                            if (err) {
                                console.log("添加错误");
                            }
                            cb();
                        }
                    );
                }
            },
            /************************************************************************/

            //增加活动内容
            function (cb) {

                if(bodyParams.activity_content_data == undefined || bodyParams.activity_content_data==""){
                    successCount = 0;
                    return cb();
                }

                var contentArr =  JSON.parse(bodyParams.activity_content_data);

                console.log(77777,contentArr)


                successCount = contentArr.length;

                completeCount = 0;

                //创建添加任务
                var tasks = [];

                //循环节点列表
                contentArr.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req,
                            {
                                url   : CORE_APIConfig.coreAPI.addBulkPurchaseActivityContent,
                                method: 'POST',
                                data  : {
                                    bpac_type   : item.type,
                                    bpac_order  : i,
                                    bpac_content: item.content,
                                    bpac_oaid   : new_oaid
                                }
                            },
                            function (err, data) {

                                //console.log(data);

                                //判断是否有错误信息
                                if (err) {
                                    //console.log("添加错误");
                                }
                                else {
                                    completeCount++;
                                }

                                cb();
                            }
                        );

                    });

                });

                async.waterfall(tasks, cb);

            },

            //判断写入结果
            function (cb) {

                if (successCount == completeCount) {
                    cb();
                }
                else {
                    return RES.response(res, false, "拼团活动新增失败，请重新尝试");
                }

            }

        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}